/**
 * 
 */
package com.yidu.erp.action.exam;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.common.CommomUtils;
import com.yidu.common.Pages;
import com.yidu.common.Tools;
import com.yidu.erp.model.BeyondStaff;
import com.yidu.erp.service.BeyondStaff.StaffService;
import com.yidu.erp.service.exam.ExamService;
import com.yidu.erp.service.stock.StockService;

/**
 * 审核Action层
 * @author zengren
 * @date 2017年11月27日
 * @version 1.0
 */
@Controller
@RequestMapping("exam")
public class ExamAction {
	
	@Resource
	private ExamService examService;
	
	@Resource
	private StockService stockService;
	
	@Resource
	private StaffService staffService;
	
	
	/**
	 * 显示总店采购
	 * @param paramMap 参数
	 * @param page 当前页数
	 * @param limit 最大行数
	 * @return
	 */
	@RequestMapping("showZPurchase")
	@ResponseBody
	public Map<String, Object> showZPurchase(@RequestParam Map<String, Object> paramMap, int page, int limit){
		Pages pages = new Pages();
		pages.setCurPage(page);
		pages.setMaxResult(limit);
		paramMap.put("pages", pages);
		return examService.showZPurchase(paramMap);
	}
	
	/**
	 * 根据采购单id查询详情（总店采购）
	 * @param praamMap
	 * @return
	 */
	@RequestMapping("findCaiGById")
	@ResponseBody
	public List<Map<String, Object>> findCaiGById(@RequestParam Map<String, Object> paramMap){
		return examService.findCaiGById(paramMap);
	}
	
	/**
	 * 显示分店采购
	 * @param paramMap 参数
	 * @param page 当前页数
	 * @param limit 最大行数
	 * @return
	 */
	@RequestMapping("showFPurchase")
	@ResponseBody
	public Map<String, Object> showFPurchase(@RequestParam Map<String, Object> paramMap, int page, int limit){
		Pages pages = new Pages();
		pages.setCurPage(page);
		pages.setMaxResult(limit);
		paramMap.put("pages", pages);
		return examService.showFPurchase(paramMap);
	}
	
	/**
	 * 根据采购单id查询详情（分店采购）
	 * @param praamMap
	 * @return
	 */
	@RequestMapping("findCaiGByIdFd")
	@ResponseBody
	public List<Map<String, Object>> findCaiGByIdFd(@RequestParam Map<String, Object> paramMap){
		return examService.findCaiGByIdFd(paramMap);
	}
	
	/**
	 * 显示总店销售
	 * @param paramMap
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("showZMarket")
	@ResponseBody
	public Map<String, Object> showZMarket(@RequestParam Map<String, Object> paramMap, int page, int limit){
		Pages pages = new Pages();
		pages.setCurPage(page);
		pages.setMaxResult(limit);
		paramMap.put("pages", pages);
		return examService.showZMarket(paramMap);
	}
	
	/**
	 * 根据销售单id查询详情（总店销售）
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("findCaiGByIdZd")
	@ResponseBody
	public List<Map<String, Object>> findCaiGByIdZd(@RequestParam Map<String, Object> paramMap){
		return examService.findCaiGByIdZd(paramMap);
	}
	
	/**
	 * 根据审核条目查询审核记录
	 * @param entryId
	 * @return
	 */
	@RequestMapping("queryExamOld")
	@ResponseBody
	public List<Map<String, Object>> queryExamOld(String entryId){
		List<Map<String, Object>> list = examService.queryExamOld(entryId);
		for(Map<String, Object> map : list){
			String roleName = this.findRoleByStaffId(map.get("EXAM_OPINOIN_ID").toString());
			map.put("roleName", Tools.decode(roleName));
		}
		return list;
	}
	
	/**
	 * 查询分店审核状态
	 * @param fdproId
	 * @return
	 */
	@RequestMapping("queryExamFdOld")
	@ResponseBody
	public List<Map<String, Object>> queryExamFdOld(String fdproId){
		List<Map<String, Object>> list = examService.queryExamFdOld(fdproId);
		for(Map<String, Object> map : list){
			String roleName = this.findRoleByStaffId(map.get("EXAM_OPINOIN_ID").toString());
			map.put("roleName", Tools.decode(roleName));
		}
		return list;
	}
	
	/**
	 * 显示生产计划
	 * @param paramMap 参数
	 * @param page 当前页数
	 * @param limit 显示最大行数
	 * @return
	 */
	@RequestMapping("showZProduce")
	@ResponseBody
	public Map<String, Object> showZProduce(@RequestParam Map<String, Object> paramMap, int page, int limit){
		Pages pages = new Pages();
		pages.setCurPage(page);
		pages.setMaxResult(limit);
		paramMap.put("pages", pages);
		return examService.showZProduce(paramMap);
	}
	
	/**
	 * 显示生产订单明细
	 * @param desingId 生产单id
	 * @return
	 */
	@RequestMapping("findSenC")
	@ResponseBody
	public List<Map<String, Object>> findSenC(String designId){
		
		return examService.findSenC(designId);
	}
	
	
	/**
	 * 审核
	 * @param paramMap 参数
	 * @return
	 */
	@RequestMapping("sk") 
	@ResponseBody
	public int sk(@RequestParam Map<String, Object> paramMap, HttpSession session){
		
		BeyondStaff staff = (BeyondStaff) session.getAttribute("staff");
		
		paramMap.put("staffId", staff.getStaffId());
		paramMap.put("staffName", staff.getStaffName());
		
		String roleName = this.findRoleByStaffId(staff.getStaffId());
		roleName = Tools.decode(roleName);
		
		int code = 0;
		
		
		
		
		if("1".equals((String)CommomUtils.roleMap.get(roleName))){//1:分店财务审核
			code = examService.fAuditFinance(paramMap);
		}else if("2".equals((String)CommomUtils.roleMap.get(roleName))){//2:分店经理审核
			if("1".equals((String)paramMap.get("state"))){
				paramMap.put("state","2");
			}
			code = examService.fAuditBoss(paramMap);
		}else if("3".equals((String)CommomUtils.roleMap.get(roleName))){//3:总店财务审核
			code = examService.zAuditFinance(paramMap);
		}else if("4".equals((String)CommomUtils.roleMap.get(roleName))){//4:总店经理审核。
			if("1".equals((String)paramMap.get("state"))){
				paramMap.put("state","2");
			}
			code = examService.zAuditBoss(paramMap);
		}
		return code;
	}
	
	/**
	 * 根据用户查询角色名称
	 * @return 角色名称
	 */
	@RequestMapping("findRoleByStaffId")
	@ResponseBody
	public String findRoleByStaffId(String staffId){
		String roleName = staffService.byStaffIdselectRoleName(staffId);
		roleName = Tools.encode(roleName);
		return roleName;
	}
	
}
